package ssf.core.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.struts.action.ActionMapping;

import ssf.core.Constants;

/**
 * 登录拦截器,AOP实现，基于Struts1
 * 
 * @author Nanlei
 * 
 */
public class LoginInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		HttpServletRequest request = null;
		ActionMapping mapping = null;
		Object[] objs = invocation.getArguments();
		for (int i = 0; i < objs.length; i++) {
			if (objs[i] instanceof HttpServletRequest) {// 获取HttpServletRequest对象
				request = (HttpServletRequest) objs[i];
			}
			if (objs[i] instanceof ActionMapping) {// 获取ActionMapping对象
				mapping = (ActionMapping) objs[i];
			}
		}
		if (request.getSession().getAttribute(Constants.LOGIN_USER) != null) {
			return invocation.proceed();
		} else {
			return mapping.findForward("forbidden");
		}
	}

}
